package com.lock.redis.lockredis.controller;

import com.lock.redis.lockredis.bean.Product;
import com.lock.redis.lockredis.service.ProductService;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RequestMapping("/test")
@RestController
public class TestController {
    //注入
//    @Autowired
//    private RedissonClient redissonClient;


    @Autowired
    private ProductService service;

    /**
     * 需求：减扣库存
     * @return
     */
    @RequestMapping("/decr")
    public boolean decrstockCount(Integer productId){
        // redis分布式锁的key
//        String key = "dec_store_lock_" + productId;
//
//        try {
//            Product product = service.getProductById(productId);
//
//            Thread.sleep(30000);
//            //如果库存为空
//            if (product.getProductStock()<=0) {
//                System.out.println("票已售完！");
//                return false;
//            }
//            //减库存
//            product.setProductStock(product.getProductStock()-1);
//            System.out.println("已购票："+Thread.currentThread()+"::"+product.getProductId()+"票号");
//            System.out.println("还剩余："+product.getProductStock()+"张票");
//            //更新库存
//            service.saveProduct(product);
//
//        }catch (Exception e) {
//            System.out.println(e.getMessage());
//        }finally {
//
//        }

        return true;
    }
}
